package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.bizactions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.parameter.retrieve.RetrieveParam;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;

/**
 * 二保反馈
 *
 * @author HYG
 */
@Slf4j
public class sbejbyfkEditLevelTwoFeedbackMgrAction extends AbstractManagerAction<Object> {
    private String ejbyfk;

    public sbejbyfkEditLevelTwoFeedbackMgrAction(
            IBEManagerContext managerContext, String ejbyfk) {
        super(managerContext);
        this.ejbyfk = ejbyfk;
    }

    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        log.error("获取到的反馈数据" + ejbyfk);
        String configID = "cde823e0-7159-4c28-b411-165d30abfe4c";
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = null;

        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(configID);
            map = mapper.readValue(ejbyfk, Map.class);
            String dataid = map.get("id").toString();
            List<Map<String, Object>> detailsList = (List<Map<String, Object>>) map.get("details");
            IEntityData data = lcp.retrieve(map.get("id").toString()).getData();

            //主表变更
            ModifyChangeDetail mainChange = new ModifyChangeDetail(data.getID());

            mainChange.getPropertyChanges().put("fkbmbh", map.get("fkbmbh"));
            mainChange.getPropertyChanges().put("fkbmmc", map.get("fkbmmc"));
            mainChange.getPropertyChanges().put("fkrbh", map.get("fkrbh"));
            mainChange.getPropertyChanges().put("fkrmc", map.get("fkrmc"));

            IEntityDataCollection yjbyfkmx = lcp.retrieve(map.get("id").toString()).getData().getChilds().get("ejbyfkmx");

            for (IEntityData entityData : yjbyfkmx) {
                ModifyChangeDetail childChange = new ModifyChangeDetail(entityData.getID());

                for (Map<String, Object> detail : detailsList) {
                    if (detail.get("id").equals(entityData.getID())) {
                        entityData.setValue("isby", detail.get("isby"));
                        entityData.setValue("qanda", detail.get("qanda"));
                        break;
                    }
                }

                mainChange.addChildChangeSet("ejbyfkmx", childChange);
                lcp.modify(mainChange);
            }
            lcp.modify(mainChange);
            lcp.save();

            // 制从数据库取数，刷新数据
            RetrieveParam retrieveParam = new RetrieveParam();
            retrieveParam.setForceFromRepository(true);
            lcp.retrieve(dataid, retrieveParam);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}